package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.StatementItemService;
import cn.wolfcode.car.business.service.StatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.List;

/**
 * @Author WaveHi
 * @Date 14/11/2021 下午 4:24
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Service
public class StatementServiceImpl extends ServiceImpl<StatementMapper, Statement> implements StatementService {
//
//    @Autowired
//    private StatementMapper statementMapper;
//
//    @Autowired
//    private StatementItemMapper statementItemMapper;

    @Autowired
    private StatementItemService statementItemService;

    @Autowired
    private IUserService userService;

    @Override
    public TablePageInfo<Statement> getPartOfStatements(StatementQuery statementQuery) {
        QueryWrapper<Statement> statementQueryWrapper = new QueryWrapper<>();
        statementQueryWrapper
                .like(statementQuery.getCustomerName() != null,
                        "customer_name",
                        statementQuery.getCustomerName())
                .like(statementQuery.getCustomerPhone() != null,
                        "customer_phone",
                        statementQuery.getCustomerPhone())
                .like(statementQuery.getLicensePlate() != null,
                        "license_plate",
                        statementQuery.getLicensePlate());
        return new TablePageInfo<>(list(statementQueryWrapper));
//        return new TablePageInfo<>(statementMapper.selectByCondition(statementQuery));
    }

    @Override
    public TablePageInfo<Statement> getPartOfStatements(Integer pageNo, Integer pageSize, StatementQuery statementQuery) {
        PageHelper.startPage(pageNo, pageSize);
        QueryWrapper<Statement> statementQueryWrapper = new QueryWrapper<>();
        statementQueryWrapper
                .like(statementQuery.getCustomerName() != null,
                        "customer_name",
                        statementQuery.getCustomerName())
                .like(statementQuery.getCustomerPhone() != null,
                        "customer_phone",
                        statementQuery.getCustomerPhone())
                .like(statementQuery.getLicensePlate() != null,
                        "license_plate",
                        statementQuery.getLicensePlate());
        List<Statement> list = list(statementQueryWrapper);
        for (Statement statement : list) {
            statement.setPayee(userService.get(statement.getPayeeId()));
        }
        return new TablePageInfo<>(list);
//        return new TablePageInfo<>(statementMapper.selectByCondition(statementQuery));
    }

    @Override
    public Boolean add(Statement statement) {
        return save(statement);
//        int insert = statementMapper.insert(statement);
//        return insert > 0;
    }

    @Override
    public Boolean update(Statement statement) {
        return updateById(statement);
//        int i = statementMapper.updateByPrimaryKey(statement);
//        return i > 0;
    }

    @Override
    public Statement getStatement(Long id) {
        Statement byId = getById(id);
        byId.setPayee(userService.get(byId.getPayeeId()));
        return byId;
//        return statementMapper.selectByPrimaryKey(id);
    }

    @Override
    public TablePageInfo<Statement> getAllStatements() {
        List<Statement> list = list();
        for (Statement statement : list) {
            statement.setPayee(userService.get(statement.getPayeeId()));
        }
        return new TablePageInfo<>(list);
//        return new TablePageInfo<>(statementMapper.selectAll());
    }

    @Override
    @Transactional
    public Boolean delete(Long[] ids) {
        Boolean result = true;
        for (Long id : ids) {
            QueryWrapper<StatementItem> statementItemQueryWrapper = new QueryWrapper<>();
            statementItemQueryWrapper.
                    eq("statement_id", id);
            result = statementItemService.remove(statementItemQueryWrapper) && result;
            result = removeById(id) && result;
        }
        return result;
//        int i = 0;
//        for (Long id : ids) {
//            i += statementItemMapper.deleteByStatementId(id);
//            i += statementMapper.deleteByPrimaryKey(id);
//        }
//        return i > 0;
    }
}
